package com.bsoft.gol.hcs.common.dao;


import com.bsoft.gol.hcs.common.entity.SysLogEntity;
import ctd.persistence.support.hibernate.HibernateSupportDelegateDAO;
import ctd.persistence.support.hibernate.template.AbstractHibernateStatelessResultAction;
import ctd.persistence.support.hibernate.template.HibernateSessionTemplate;
import ctd.persistence.support.hibernate.template.HibernateStatelessResultAction;
import ctd.util.converter.ConversionUtils;
import hcn.bean.SysLogRequest;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Query;
import org.hibernate.StatelessSession;
import org.hibernate.criterion.CriteriaSpecification;
import com.bsoft.gol.hcs.utils.CommonUtils;
import com.bsoft.gol.hcs.utils.DateUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public abstract class SysLogDAO extends HibernateSupportDelegateDAO<SysLogEntity> {

    public Integer queryLogCount(SysLogRequest req) {
        HibernateStatelessResultAction<Integer> action = new AbstractHibernateStatelessResultAction<Integer>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = getSysLogCountStringBuffer(req);
                Query sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = sqlQuery.list();
                if (CommonUtils.isNotEmpty(dbList)) {
                    setResult(Integer.valueOf(String.valueOf(dbList.get(0).get("countValue"))));
                } else {
                    setResult(0);
                }
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }


    public List<SysLogEntity> queryLogList(final SysLogRequest req) {
        HibernateStatelessResultAction<List<SysLogEntity>> action = new AbstractHibernateStatelessResultAction<List<SysLogEntity>>() {
            @Override
            public void execute(StatelessSession ss) {
                StringBuffer stringBuffer = getSysLogListStringBuffer(req);

                Query sqlQuery = ss.createSQLQuery(stringBuffer.toString());
                int first = req.getPageNo() > 0 ? (req.getPageNo() - 1) * req.getPageSize() : 0;
                sqlQuery.setFirstResult(first);
                sqlQuery.setMaxResults(req.getPageSize());
                sqlQuery.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
                List<Map> dbList = sqlQuery.list();
                setResult(mapToList(dbList));
            }
        };
        HibernateSessionTemplate.instance().execute(action);
        return action.getResult();
    }

    private StringBuffer getSysLogListStringBuffer(SysLogRequest request) {

        StringBuffer stringBuffer = new StringBuffer();

        stringBuffer.append("select t.logUnid,t.organizationId,t.logCractUnid,t.logCractUname,t.logType,t.logmodulename as logModuleName ,t.logMethodSign,");
        stringBuffer.append(" t.loginName,t.logUserId,t.logUserName,t.roleId,t.roleName,t.logDoctorCode,t.patientMedicalCardNumber,t.patientMedicalCardType,");
        stringBuffer.append(" t.logServerName,t.logDesc,t.logparameter ,t.logCdate,t.logIp,t.logMACAddress,t.logresCode,t.logClentOS ,t.logClentBrowser");
        stringBuffer.append(" from sys_log t  where 1=1");
        if (StringUtils.isNotEmpty(request.getOrganizationId())) {
            stringBuffer.append(" and t.organizationId ='" + request.getOrganizationId() + "'");
        }
        if (StringUtils.isNotEmpty(request.getLogCractUname())) {
            stringBuffer.append(" and t.logCractUname like '" + request.getLogCractUname() + "%' ");
        }
        if (StringUtils.isNotEmpty(request.getLoginName())) {
            stringBuffer.append(" and t.loginName like '" + request.getLoginName() + "%' ");
        }

        if (StringUtils.isNotEmpty(request.getLogUserName())) {
            stringBuffer.append(" and t.logUserName like '" + request.getLogUserName() + "%' ");
        }
        if (StringUtils.isNotEmpty(request.getLogModuleName())) {
            stringBuffer.append(" and t.logmodulename = '" + request.getLogModuleName() + "' ");
        }
        if (request.getLogcDateStart() != null) {
            stringBuffer.append(" and t.logCdate >='").append(DateUtil.dateFormate(request.getLogcDateStart(), "yyyy-MM-dd")).append(" 00:00:00'");
        }
        if (request.getLogcDateEnd() != null) {
            stringBuffer.append(" and t.logCdate <='").append(DateUtil.dateFormate(request.getLogcDateEnd(), "yyyy-MM-dd")).append(" 23:59:59'");
        }
        stringBuffer.append("  order by logCdate desc ");

        return stringBuffer;
    }


    private StringBuffer getSysLogCountStringBuffer(SysLogRequest request) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("SELECT count(1) countValue");
        stringBuffer.append(" from sys_log t  where 1=1");
        if (StringUtils.isNotEmpty(request.getOrganizationId())) {
            stringBuffer.append(" and t.organizationId ='" + request.getOrganizationId() + "'");
        }
        if (StringUtils.isNotEmpty(request.getLogCractUname())) {
            stringBuffer.append(" and t.logCractUname like '" + request.getLogCractUname() + "%' ");
        }
        if (StringUtils.isNotEmpty(request.getLoginName())) {
            stringBuffer.append(" and t.loginName like '" + request.getLoginName() + "%' ");
        }
        if (StringUtils.isNotEmpty(request.getLogUserName())) {
            stringBuffer.append(" and t.logUserName like '" + request.getLogUserName() + "%' ");
        }
        if (StringUtils.isNotEmpty(request.getLogModuleName())) {
            stringBuffer.append(" and t.logmodulename = '" + request.getLogModuleName() + "' ");
        }
        if (request.getLogcDateStart() != null) {
            stringBuffer.append(" and t.logCdate >='").append(DateUtil.dateFormate(request.getLogcDateStart(), "yyyy-MM-dd")).append(" 00:00:00'");
        }
        if (request.getLogcDateEnd() != null) {
            stringBuffer.append(" and t.logCdate <='").append(DateUtil.dateFormate(request.getLogcDateEnd(), "yyyy-MM-dd")).append(" 23:59:59'");
        }
        return stringBuffer;
    }

    private List<SysLogEntity> mapToList(List<Map> maps) {
        List<SysLogEntity> sysLogEntityList = new ArrayList<>();
        for (Map map : maps) {
            sysLogEntityList.add(ConversionUtils.convert(map, SysLogEntity.class));
        }
        return sysLogEntityList;
    }

}
